﻿using System;
using System.ComponentModel;
using System.Text;

namespace ShareHelper
{
    /// <summary>
    /// 枚举类 Helper
    /// </summary>

    public static class EnumHelper
    {
        /// <summary>
        ///  判断是否为 枚举
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEnum<T>(this object type, out T value)
            where T : struct
        {
            if (Enum.TryParse(type.ToString(), out value))
            {
                return Enum.IsDefined(typeof(T), value);
            }
            return false;
        }

        /// <summary>
        ///  判断是否为 枚举
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsEnum<T>(this object type)
            where T : struct
        {
            return IsEnum<T>(type, out _);
        }

        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="en">枚举</param>
        /// <returns></returns>
        public static string GetDescription(this Enum en)
        {
            string Name = en.ToString();
            var field = en.GetType().GetField(Name);
            var objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
            if (objs.Length == 0) return Name;    //当描述属性没有时，直接返回名称
            var descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }
    }

    /// <summary>
    /// 异常扩展
    /// </summary>
    public static class ExceptionHelper
    {
        /// <summary>
        /// 获取异常日志
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static string GetErr(this Exception exception)
        {
            //var msg = exception + "";
            //if (msg.IsNullOrEmpty()) return null;
            //var ss = msg.Split(Environment.NewLine);
            //var ns = ss.Where(e =>
            //!e.StartsWith("---") &&
            //!e.Contains("System.Runtime.ExceptionServices") &&
            //!e.Contains("System.Runtime.CompilerServices"));
            //msg = string.Join(Environment.NewLine,ns);
            if (exception == null)
                return string.Empty;
            var str = new StringBuilder("\r\n");
            str.Append("//*************************************\r\n");
            str.Append($"异常时间：{TimerEx.Now:yyyy-MM-dd HH:mm:ss}\r\n");
            str.Append($"异常信息：{exception.Message}\r\n");
            str.Append($"错误源：{exception.Source}\r\n");
            str.Append($"运行类型：{exception.GetType()}\r\n");
            str.Append($"异常函数：{exception.TargetSite}\r\n");
            str.Append($"堆栈信息：{exception.StackTrace}\r\n");
            return str.ToString();
        }

        /// <summary>是否对象已被释放异常</summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static bool IsDisposed(this Exception ex) => ex is ObjectDisposedException;
    }
}